﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CdcSoftware.Pivotal.Applications.Core.Client;
using System.Data;
using CdcSoftware.Pivotal.Applications.Core.Common;
using CdcSoftware.Pivotal.Engine;
using CdcSoftware.Pivotal.Engine.Client.Services.Interfaces;

namespace Pcm.Retail.Client.FormTasks
{
    public partial class RE_AddInstallBaseItem: FormClientTask
    {
        private DataTable OrderProducts = null;

        #region Constructor
        /// <summary>
        /// Default constructor where secondary fields are being initialized to be used across assembly
        /// </summary>
        public RE_AddInstallBaseItem()
        {

        }
        #endregion

        public override void OnFormInitialized()
        {
            try
            {
                if (this.TransitionPointParameter.UserDefinedParametersNumber == 1)
                {
                    OrderProducts = (DataTable)this.TransitionPointParameter.GetUserDefinedParameter(1);
                }                

                this.TaskPad.Visible = false;
                Globals.RemoveButtonBarButton(new string[] { "Save", "Apply", "Cancel", "Delete", "Refresh" });
                this.FormView.OnDirtyStateChanged += new EventHandler<Microsoft.Practices.CompositeUI.Utility.DataEventArgs<bool>>(FormView_OnDirtyStateChanged); 

                //if (Convert.IsDBNull(this.PrimaryDataRow["RE_RAE_Serial_Number"]))
                //    this.FormControl.GetControlByName("PivotalButton1").Enabled = false;
                //else
                //    this.FormControl.GetControlByName("PivotalButton1").Enabled = true;
                this.EnableDisableAddItemButton();
            }
            catch(Exception exc)
            {
                Globals.HandleException(exc,true);
            }
        }

        private void FormView_OnDirtyStateChanged(object sender, Microsoft.Practices.CompositeUI.Utility.DataEventArgs<bool> e)
        {
            this.FormView.Dirty = false;
        }


       

        /// <summary>
        /// 
        /// </summary>
        /// <param name="appleCareService"></param>
        /// <param name="lobId"></param>
        /// <returns></returns>
        private AppleCare.authenticateResponseType CallAppleCareAuthenticate(AppleCare.GsxWSApacResellerService appleCareService, DataRow drLOB)
        {
            try
            {
                AppleCare.authenticateRequestType ar = new AppleCare.authenticateRequestType();

                ar.userId = TypeConvert.ToString(drLOB["RE_RAE_User_Id"]);
                ar.password = TypeConvert.ToString(drLOB["RE_RAE_Password"]);
                ar.serviceAccountNo = TypeConvert.ToString(drLOB["RE_RAE_ServiceAccountNo"]);
                ar.languageCode = TypeConvert.ToString(drLOB["RE_RAE_LanguageCode"]);
                ar.userTimeZone = TypeConvert.ToString(drLOB["RE_RAE_UserTimeZone"]);

                AppleCare.authenticateResponseType response = appleCareService.Authenticate(ar);

                return response;

            }
            catch (Exception exc)
            {
                throw new PivotalApplicationCoreException(exc.Message, exc);
            }
        }

        private AppleCare.resellerWarrantyStatusResponseType CallAppleCareResellerWarrantyStatus()
        {
            try
            {
                AppleCare.GsxWSApacResellerService appleCareService = new AppleCare.GsxWSApacResellerService();
                AppleCare.resellerWarrantyStatusRequestType warrantyRequest = new AppleCare.resellerWarrantyStatusRequestType();

                AppleCare.gsxUserSessionType userSession = new AppleCare.gsxUserSessionType();
                if (Globals.AppleCareUserSessionId != string.Empty)
                {
                    userSession.userSessionId = Globals.AppleCareUserSessionId;

                    if (userSession.userSessionId != string.Empty)
                    {
                        warrantyRequest.userSession = userSession;

                        AppleCare.resellerUnitDetailType unitDetail = new AppleCare.resellerUnitDetailType();
                        unitDetail.serialNumber = TypeConvert.ToString(this.PrimaryDataRow["RE_RAE_Serial_Number"]);

                        warrantyRequest.unitDetail = unitDetail;

                        AppleCare.resellerWarrantyStatusResponseType warrantyResponse = appleCareService.ResellerWarrantyStatus(warrantyRequest);

                        return warrantyResponse;
                    }
                    return null;
                }
                else
                {
                    PivotalMessageBox.Show("Unable to communicate with Apple Web Service." + Environment.NewLine
                        + "Please contact System Administrator.", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
                    return null;
                }
            }
            catch (Exception exc)
            {
                throw new PivotalApplicationCoreException(exc.Message, exc);
            }
        }

        /// <summary>
        /// This method enable/disalbe the AddItem Button depending upon both the Serial No. and Product ID.
        /// </summary>        
        /// <history>
        /// #Revision   Date            Author      Description
        /// 6.0.12.0    2013-06-24      BS          Initial version for Retail.
        /// </history>
        public virtual void EnableDisableAddItemButton()
        {
            try
            {
                if (!Convert.IsDBNull(this.PrimaryDataRow["RE_RAE_Serial_Number"]) && !Convert.IsDBNull(this.PrimaryDataRow["Product_Id"]))
                    this.FormControl.GetControlByName("PivotalButton1").Enabled = true;
                else
                    this.FormControl.GetControlByName("PivotalButton1").Enabled = false;

            }
            catch (Exception exc)
            {
                Globals.HandleException(exc, true);
            }
        }

    }
}
